package metrics

import (
	"fmt"
	dto "github.com/prometheus/client_model/go"
)

var oldValue float64

func GetContainerSpecialCpuMetricUsage(mf *dto.MetricFamily, cpus string, container string) float64 {
	fmt.Printf("\ncpus %v container %v", cpus, container)

	var usage float64
	for _, m := range mf.Metric {
		var cpu, name string
		for _, l := range m.Label {
			switch *l.Name {
			case "cpu":
				cpu = *l.Value
			case "name":
				name = *l.Value
			}
		}

		if cpu == cpus && name == container {
			usage = *m.Counter.Value
		}
	}

	//if oldValue2 == 0 {
	//	oldValue2 = oldValue
	//	if oldValue == 0 {
	//		oldValue = usage
	//		return -1
	//	}
	//}

	if oldValue == 0 {
		oldValue = usage
		return -1
	}

	res := (usage - oldValue) / 2
	//oldValue2 = oldValue
	oldValue = usage
	return res
}
